package com.ycy;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;

public class TomcatDemo {

    public static void main(String[] args) throws LifecycleException {

        Tomcat tomcat = new Tomcat();

        Connector connector = new Connector("HTTP/1.1");
        connector.setPort(8080);
        tomcat.setConnector(connector);

//        tomcat.setBaseDir(System.getProperty("user.dir"));
//        StandardContext context = new StandardContext();
////        // 设置资源路径
////        context.setDocBase((System.getProperty("user.dir")+"\\src\\main\\resources")
////                .replace("\\","/"));
//        // 设置应用路径
//        context.setPath("");
//        context.addLifecycleListener(new Tomcat.FixContextListener());
        // 将context加入tomcat

        // 在context中创建表示servlet的Wrapper并返回
//        Wrapper testServlet = context.createWrapper();
//        testServlet.setName("test");
//        testServlet.setServletClass("com.wenliang.servlet.TestServlet");
//        testServlet.addInitParameter("fork", "false");
//        context.addChild(testServlet);

        // 设置 webapp 目录
        String webappDir = "src/main/webapp";
        tomcat.addWebapp("/", new java.io.File(webappDir).getAbsolutePath());

        // 加载 web.xml
//        String configFilePath = new java.io.File(webappDir + "/WEB-INF/web.xml").getAbsolutePath();
//        tomcat.getHost().setConfigFile(new java.net.URI(configFilePath).toURL());

        tomcat.start();
        tomcat.getServer().await();
    }

}
